package com.lsh.oms.provider.service.query;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.order.delivery.OrderSendDetailResDTO;
import com.lsh.oms.api.service.query.IFindOrderByPmsService;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.exception.ExceptionStatus;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.model.order.OrderShippingDetail;
import com.lsh.oms.core.model.order.OrderShippingHeadWithBLOBs;
import com.lsh.oms.core.service.other.ShippingOrderService;
import com.lsh.oms.core.service.payment.PaymentService;
import com.lsh.oms.core.service.tool.ToolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/6/24
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service(protocol = "rest")
@Path("order/pms/query")
@Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
@Slf4j
@Deprecated
public class FindOrderByPmsServiceImpl implements IFindOrderByPmsService {

    @Autowired
    private ShippingOrderService shippingService;

    @Autowired
    private PaymentService paymentService;

    @GET
    @Path("deliveryDetailsByOrderCode")
    @Override
    public String getDeliveryDetails(@QueryParam("order_code") Long orderCode) {
        CommonResult<List<OrderSendDetailResDTO>> resps = new CommonResult<>();
        try {
            OrderShippingHeadWithBLOBs osh = shippingService.getBolbs(orderCode);
            if (osh == null) {
                throw new BusinessException(ReturnCode.REQUEST_FAIL, "程序在努力工作,发货进行中,请稍后再试");
            }
            List<OrderShippingDetail> shippingDetails = shippingService.getDetails(osh.getShippingOrderId());

            List<OrderSendDetailResDTO> sendDetailResponeDTOList = new ArrayList<>(shippingDetails.size());
            OrderSendDetailResDTO sendDetail;
            for (OrderShippingDetail detail : shippingDetails) {

                sendDetail = new OrderSendDetailResDTO();
                BeanUtils.copyProperties(detail, sendDetail);
                sendDetailResponeDTOList.add(sendDetail);
            }
            resps.setMessage("success.");
            resps.setCode(ReturnCode.REQUEST_SUCCESS);
            resps.setData(sendDetailResponeDTOList);
        } catch (BusinessException e) {
            log.error("[getDeliveryDetails] 业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
            resps.setCode(e.getCode());
            resps.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("[getDeliveryDetails] 异常code : " + ExceptionStatus.E3001001.getCode() + ",异常信息: " + e.getMessage(), e);
            resps.setCode(ExceptionStatus.E3001001.getCode());
            resps.setMessage(ExceptionStatus.E3001001.getMessage());
        }

        return JSON.toJSONString(resps);
    }

    /**
     *
     * @param orderCodes
     * @return
     */
    @GET
    @Path("getOrderHeadList")
    @Override
    public String getOrderHeadList(@QueryParam("orderCodes") String orderCodes) {
        CommonResult commonResult = new CommonResult();
        if (StringUtils.isBlank(orderCodes)) {
            commonResult.setCode(ReturnCode.REQUEST_PARAMS_IS_NULL);
            commonResult.setMessage("request params is null!");
            return JSON.toJSONString(commonResult);
        }

        try {
            JSONArray orderCodeArray = JSONArray.parseArray(orderCodes);

            if(orderCodeArray.size() > 100){
                throw new BusinessException(ReturnCode.REQUEST_FAIL, "查询数据量太大，最多支持100个订单");
            }

            List<Long> codes = new ArrayList<>();
            for (int i = 0; i < orderCodeArray.size(); i++) {
                codes.add(orderCodeArray.getLong(i));
            }

            List<OrderHead> orderHeads = paymentService.getOrderHeadsByOrderCodesWithDetails(codes);
            commonResult.setData(orderHeads);
            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage("success");
            return JSON.toJSONString(commonResult, ToolService.valueFilter, SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.WriteNullNumberAsZero);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            commonResult.setCode(ReturnCode.SYS_ERROR);
            commonResult.setMessage(e.getMessage());
            return JSON.toJSONString(commonResult);
        }
    }

}
